/*
 * 微商城
 * @author <!-- blekfan 16217439342qq.com -->
 */
//分类树
;(function($){
	$.toggleCate=function(e)
	 {
	    var id=e;
	    $('.pid_'+id).toggle();
	    $('.pid_'+id).css('font-weight','bold');
	    var $childcate=$('.child_'+id)
	    $childcate.toggle();
	    $childcate.css('margin-left','20px');
	    $childcate.css('color','#FFA500');
	 }
})(jQuery)
//商品详情加入购物车模块
;(function($){
	$.itemDetail={
		getProduct:function(e){
			var itemid=(e !=false)?e:0;
			if(e ==false || e==null ) return false;
			//获取商品规格
			$.ajax({
				type:'post',
				url:"/vshop.php/Home/ItemDetail/getSpec",
				data:{'itemid':itemid},
				dataType:'json',
				success:function(message){
						new Vue({
							el: '#app',
							data: { todos: message.products,},
							methods:
							{
								toggleprice:function(index)
								{
  									if(message.status)
  									{
  										//存在规格设置相应的颜色对应的价格和库存
  										var color_price=this.todos[index].price;
  										$('.price').html(color_price);
  										$('.inventory').html(this.todos[index].inventory);
  									}
  									
  									//选中此颜色
  									$('.color').not(index).removeClass('current');
  									$('.color').eq(index).addClass('current');
  									//设置productid
  									$("input[name='productid']").val(this.todos[index].productid);
  								}
  							},
  						})
						return '';
					
				}
			})

		},
		//改变数量
		totalprice:function(e){
			var action=(e!=false)?e:0
			if(action==false){return false;}
			switch(action){
				case'add':
				var itemnumber=Number($("input[name='itemnumber']").val());
				var itemnumber=1+itemnumber;
				$("input[name='itemnumber']").val(itemnumber);
				break;
				case 'reduce':
				var itemnumber=$("input[name='itemnumber']").val();
				if(itemnumber<=1){return false;}
				var itemnumber=Number($("input[name='itemnumber']").val());
				var itemnumber=itemnumber-1;
				$("input[name='itemnumber']").val(itemnumber);
				break;
			}
		},
		//加入购物车
		addcart:function(){
			var itemid=$("input[name='itemid']").val();
			var productid=$("input[name='productid']").val();
			var itemnumber=$("input[name='itemnumber']").val();
			if(itemnumber==false){alert('请选择数量'); return false;}
			$.ajax({
				url:'/vshop.php/Home/Cart/addCart',
				type:'post',
				data:{'itemid':itemid,'productid':productid,'itemnumber':itemnumber},
				dataType:'json',
				success:function(message)
				{
					/*if(message.status){
						//更改购物车数量
						$('span.num').html(message.);
						var answer=confirm('确定去结算?');
						
						if(answer){ 
							window.location.href='/vshop.php/Home/Cart/index'
							return false;
						}else{
							return false;
						}
						
					}
					alert(message.msg);
					return false;*/
					switch(message.status){
						case 0:
							alert(message.msg);
							return false;
						break;
						case 3:
							answer=confirm('请先登录/注册用户');
							if(answer){window.location.href='/vshop.php/Home/Login/index'}
								return false;
						break;
						case 1:
							//更改购物车数量
							$('span.num').html(message.carttotal);
							var answer=confirm('确定去结算?');
							if(answer){window.location.href='/vshop.php/Home/Cart/index';}
							return false;
						break;
					}
				}
			})
			//window.location.href='';
		}

	}
})(jQuery)
/*
 *用户登录注册
 */
 ;(function($)
 {
 	$.user={
 		submit:function()
 		{
 			var username=$("input[name='username']").val();
 			var password=$("input[name='password']").val();
 			if(username==false || password==false )
 			{ 
 				$('.hint').html('用户名密码必填');

 				$(".md-box").show();

 				$(".bt-close").click(function(index)
 				{
 					$(".md-box").hide();
 				})
 				$(".bt-2").click(function(index)
 				{
 					$(".md-box").hide();
 				})
 				return false;
 			}
 			$.ajax({
 				url:'/vshop.php/Home/Login/verifyUser',
 				method:'post',
 				data:{'username':username,'password':password},
 				dataType:'json',
 				success:function(message){
 					if (message.status)
 					{
						window.location.href='/vshop.php/Home/User/index'
						return false;
					}
					//faild
					$('.tips').html(message.msg);
				}


			})

 		},
 		
 	}
 })(jQuery)
 //createorder 模块
;(function($){

	$.Order={

		confirm:function(){
			//获取用户信息
			var $consignee=$('.consignee').html();
			var $mobile=$('.phone-num').html();
			var $province=$('.province').html();
			var $city=$('.city').html();
			var $county=$('.county').html();
			var $address=$('.address').html();
			if( $consignee==false||$mobile==false|| $province==false||$city==false||$county==false||$address==false){alert('请选择默认收货地址!');return false;}
			
			$.ajax({
				type:'post',
				url:'/vshop.php/Home/Order/confirmOrder',
				data:{'consignee':$consignee,'mobile':$mobile,'province':$province,'city':$city,'county':$county,'address':$address},
				dataType:'json',
				success:function(message){
					switch(message.status)
					{
						case 0:
						alert(message.msg);	
						break;
						case 2:
						alert(message.msg);
						break;
						case 1:
						window.location.href='/vshop.php/Home/Order/payStyle?orderid='+message.orderid;
						break;
					}

					return false;
					
					
				}
			})
			
		}
	}
})(jQuery)
